﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using FTchina;

public partial class UI_userselecter : System.Web.UI.Page
{
    string type = string.Empty;
    string checkbox = string.Empty;
    Boolean flag = true;
    protected void Page_Load(object sender, EventArgs e)
    {
        Response.Cache.SetCacheability(HttpCacheability.NoCache);
        Response.Expires = 0;
        Response.Buffer = true;
        Response.ExpiresAbsolute = DateTime.Now.AddSeconds(-1);
        Response.AddHeader("pragma", "no-cache");
        Response.CacheControl = "no-cache";

        type = FTchina.basic.FilterSQL(Request["act"]);
        checkbox = FTchina.basic.FilterSQL(Request["checkbox"]);

        LoadList();

    }


    #region 加载分类



    /// <summary>
    /// 加载整个分类表
    /// </summary>
    private void LoadList()
    {

        litlist.Text = "";

        int MTcount = 0;//执行遍历的次数
        //开始迭代加载
        MakeTr("0", MTcount);




    }
    /// <summary>
    /// 装配每一行
    /// </summary>
    /// <param name="id">上级ID</param>
    /// <param name="count">迭代次数</param>
    private void MakeTr(string id, int count)
    {

        DataView dv = new DataView(shujuku.GetDataTable("select * from userlist where parentid=" + id + " order by paixu desc,id desc"));

        foreach (DataRowView drv in dv)
        {
            switch (type)
            {
                case "single": //显示可登陆员工
                    {
                        flag = SingleLogin(drv["id"].ToString());
                        break;
                    }
                case "office"://显示部门
                    {
                        flag = Office(drv["id"].ToString());
                        break;
                    }
                case "AllSingle"://显示所有员工
                    {
                        flag = AllSingle(drv["id"].ToString());
                        break;
                    }
                    
                default://默认显示可登陆员工
                    {
                        flag = SingleLogin(drv["id"].ToString());
                        break;
                    }
            }

            string radio = (flag) ? "<input onclick=cc('" + drv["id"] + "','" + drv["master_name"] + "') type=radio id=r" + drv["id"] + " value=" + drv["id"] + " name=ruser >" : "";

            string check = (flag) ? "<input type=checkbox name=cuser id=c" + drv["id"] + " value=" + drv["id"] + "|" + drv["master_name"] + " />" : "";


            //根据传入参数确定显示radio或者checkbox
            string CheckBoxORRadio = (checkbox == "checkbox") ? check : radio;

            //形成表格
            litlist.Text += "<tr><td>" + MakeFelgefu(count) + drv["master_name"].ToString() + "</td>" +
           "<td>" + CheckBoxORRadio + "</td></tr>";
            //再次遍历
            MakeTr(drv["id"].ToString(), count + 1);

        }

    }
    /// <summary>
    /// Make分隔符
    /// </summary>
    /// <param name="count"></param>
    /// <returns></returns>
    private string MakeFelgefu(int count)
    {
        string Returnwords = string.Empty;
        if (count == 0)
        {
            Returnwords = "";
        }

        else
        {
            Returnwords = ("├").PadLeft(count, '　');
        }
        return Returnwords;
    }

    #endregion


    #region 反馈模式  部门|所有员工|可登陆员工

    /// <summary>
    /// 检查是否可登陆最小项，即员工是否有登录权限
    /// </summary>
    /// <returns></returns>
    private Boolean SingleLogin(string id)
    {
        return (!HasChind(id) && HasLoginName(id));
    }
    /// <summary>
    /// 获取所有员工
    /// </summary>
    /// <returns></returns>
    private Boolean AllSingle(string id)
    {
        return (!HasChind(id) );
    }

    /// <summary>
    /// 检查是否部门项，即包含子项，没有登录权限
    /// </summary>
    /// <param name="id"></param>
    /// <returns></returns>
    private Boolean Office(string id)
    {
        return (HasChind(id) && !HasLoginName(id));

    }
   


    /// <summary>
    /// 查看是不是最底层项
    /// </summary>
    /// <param name="id"></param>
    /// <returns></returns>
    private Boolean HasChind(string id)
    {
        return shujuku.testread("select id from userlist where parentid='" + id + "'");
    }


    /// <summary>
    /// 查看是否有登录权限
    /// </summary>
    /// <param name="id"></param>
    /// <returns></returns>
    private Boolean HasLoginName(string id)
    {
        return shujuku.testread("select link from master where link='" + id + "'");
    }


    #endregion


   





}